# coding=utf-8

import os
import urllib
import urllib.request
import http.cookiejar
import simplejson
import tempfile
import sys



def text_wrapped_by(start, end ,content):
# get the text wrapped by start、end in content'''
    si = content.find(start)
    if si != -1:
        si += len(start)
    ei = content.find(end, si)
    if ei != -1:
        return content[si:ei]
    return None


class BaiduUser(object):

    COOKIE_PATH = "cookies"

    LOGIN_URL = "http://passport.baidu.com/?login"
    LOGIN_IMG_URL = "http://passport.baidu.com/?verifypic"
    POST_URL = "http//tieba.baidu.com/f/commit/thread/add"
    THREAD_URL = "http://tieba.baidu.com/f/commit.thread/add"
    TBS_URL = "http://tieba.baidu.com/dc/common/tbs"
    VCODE_URL = "http://tieba.baidu.com/f/user/json_vocde? lm=%s&rs10=2&rs1=0&t=0.7"
    IMG_URL = "http://tieba.baidu.com/cgi-bin/genimg?%s"

    LOGIN_ERR_MSGS = {
    "1": "用户名格式错误，请重新输入",
    "2": "用户不存在",
    "3": "",
    "4": "登录密码错误，请重新输入",
    "5": "今日登陆次数过多",
    "6": "验证码不匹配，请重新输入验证码",
    "7": "登录时发生未知错误，请重新输入",
    "8": "登录时发生未知错误，请重新输入",
    "16": "对不起，您现在无法登陆",
    "51": "改手机号未通过验证",
    "52": "该手机已经绑定多个用户",
    "53": "手机号码格式不正确",
    "58": "手机号格式错误，请重新输入",
    "256": "",
    "257": "请输入验证码",
    "20": "此账号已登录人数过多",
    "default": "登录时发生未知错误，请重新输入"
    }
    POST_ERR_MSGS = {
    "38": "验证码超时，请重新输入",
    "40": "验证码输入错误，请您返回后重新输入",
    "703": "为了减少恶意灌水和广告贴，本吧被设置为仅本吧会员才能发帖",
    "704": "为了减少恶意灌水和广告贴，本吧被设置为仅本吧管理团队才能发帖，给您带来的不便表歉意",
    "705": "本吧当前只能浏览，不能发帖！",
    "706": "抱歉，本帖暂时无法回复。",
    "900": "为抵御挖坟威海，本吧吧主已放出贴吧神兽--超级静止蛙，本帖暂时无法回复。",
    }

    def __init__(self, username, password):
        self.username = username
        self.password = password
        # if not os.path.exists(self.COOKIE_PATH):
        #     os.makedirs(self.COOKIE_PATH)
        # self.COOKIE_FILE = os.path.join(self.COOKIE_PATH, str(hash(self.username)))
        # self.cj = http.cookiejar.MozillaCookieJar(self.COOKIE_FILE)
        self.cj = http.cookiejar.MozillaCookieJar('cookie.txt')
        self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
        self.opener.addheaders = [("User-agent", "Mozilla/5.0 (x11; U; FreeBSD i386; en-US; rv:1.9.1) Gecko/20090704 Firefox/3.5"), ("Accept", "*/*")]

    def is_login(self):
    # 通过访问TBS判断是否是登陆状态'''
        content = self.opener.open(self.TBS_URL).read()
        content = simplejson.loads(content)
        print(content["is_login"])
        return content["is_login"]

    def login(self, verify_code=""):
    # '''登陆'''
        if self.is_login():
            print("登陆成功")
            return
        print("登录失败")
        return
        # print(u"正在登陆 %s ..." % self.username)
        # if os.path.exists('cookie.txt'):
        #     try:
        #         self.cj.load()
        #         print("发现上次登陆的cookie,使用旧cookie。")
        #         if self.is_login():
        #             return
        #         print("cookie已失效，重新登陆。")
        #     except Exception:
        #         pass
        #
        # login_params = {
        # 'mem_pass': 'on',
        # 'username': self.username.encode("gbk"),
        # 'password': self.password,
        # 'verifycode': verify_code
        # }
        # result = self.opener.open(self.LOGIN_URL, urllib.parse.urlencode(login_params))
        # # 检查是否登录成功
        # if "USERID" in [ x.name for x in self.cj ]:
        #     self.cj.save()
        # return

        # content = result.read().decode("gbk", "ignore")
        # body = text_wrapped_by('<body onload="sett_pwd_load()">', '</body>', content)
        # err_code = text_wrapped_by("get_err_str(", ",", body)
        # # 如果是需要输入验证码
        # if err_code == '257':
        #     print("需要输入验证码，重新登录中...")
        #     verify_code = self.open_img(self.LOGIN_IMG_URL)
        #     return self.login(verify_code=verify_code)
        #
        # err_msg = self.LOGIN_ERR_MSGS.get(err_code, self.LOGIN_ERR_MSGS["default"])
        # raise LoginError(err_msg)

    # def reply(self, url, content):
    #     print(u"%s 正在发表回帖(%s)..." % (self.username, url))
    #     return self._post("post", url, content)

    def post(self, url, title, content):
        print(u"%s 正在发表主帖(%s)..." % (self.username, url))
        return self._post("thread", url, title, content)

    def _post(self, ptype, *args):
        url=''
        title = ''
        conteng = ''
        if ptype == "thread":
            url, title, content = args
        else:
            url, content = args
            post_content = self.opener.open(url).read().decode("gbk", "ignore")
            tieba_name = self.get_tieba_name(post_content)
            if not tieba_name:
                return

            tieba_name = tieba_name.encode("utf-8")
            tbs = self.get_tbs(post_content)
            tid = self.get_tid(post_content)
            fid = self.get_fid(post_content)
            vcode_md5 = self.get_vcode(fid)

            # 获得验证码而且写入本地文件
            vcode = self.open_img(self.IMG_URL % vcode_md5)

            post_args = {
                "hasuploadpic": 0,
                "ie": "utf-8",
                "picsign": "",
                "rich_text": 1,
                "tfrom": 1,
                "quote_id": 0,
                "user_type": 0,
                "floor_num": "",
                "content": "%s<br>" % content,
                "kw": tieba_name,
                "fid": fid,
                "tbs": tbs,
                "tid": tid,
                "vcode": vcode,
                "vcode_md5": vcode_md5,
                }
            if ptype == "thread":
                post_args.update(
                title=title,
                useSignName="on",
                floor_num=0
                )
            ret = self.opener.open(self.POST_URL if ptype == "post" else self.THREAD_URL, urllib.parse.urlencode(post_args))
            ret = simplejson.loads(ret.read())
            ret_no = ret["no"]
            if ret_no == 0:
                print("发帖成功！(%s)..." % url)
                return
            err_msg = self.POST_ERR_MSGS.get(str(ret_no))
            if err_msg:
                print(err_msg)
            else:
                print("发帖失败。(%s)..." % ret_no)
            if ret_no in (38, 40):
                return self._post(ptype, *args)

@staticmethod
def get_tieba_name(content):
    ret = text_wrapped_by("<title>", u"吧_贴吧", content)
    if ret:
        return ret.split("_")[-1]
    return None

@staticmethod
def get_tbs(content):
    return text_wrapped_by('tbs:"', '"', content)

@staticmethod
def get_tid(content):
    ret = text_wrapped_by("tid:'", "'", content)
    return ret

@staticmethod
def get_fid(content):
    ret = text_wrapped_by("fid:'", "'", content)
    return ret

def get_vcode(self, fid):
    content = self.opener.open(self.VCODE_URL % fid).read()
    content = simplejson.loads(content)
    return content['data']['vcodestr']

def open_img(self, url):
#获得验证码

    img = self.opener.open(url)
    content = img.read()
    if not content:
        return ""

    f = tempfile.NamedTemporaryFile()
    f.write(content)
    f.flush()
    return
    # s = Shower(False, image=f.name)
    # s.start()
    #
    # return s.result.encode("utf-8")

if __name__ == '__main__':
    username = "_SissA"
    password = "fanmao610"
    u = BaiduUser(username, password)
    u.login()
    # 发表主贴
    u.post("http://tieba.baidu.com/f?kw=残9527", "this is a test(2)", "Hello, world.")
